package controllers

import (
	"gitee.com/igolang/imoney/internal/app/system/entities"
	servicesDict "gitee.com/igolang/imoney/internal/app/system/services/dict"
	"gitee.com/igolang/imoney/internal/common/httpResp"
	"gitee.com/igolang/imoney/internal/models/dao"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

var Dict = &dict{}

type dict struct{}

func (c *dict) DataList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := servicesDict.DictData.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *dict) DataExport(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := servicesDict.DictData.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}
	httpResp.Export(ctx, b, "xlsx")
}

func (c *dict) DataSave(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := servicesDict.DictData.Save(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *dict) DataDetail(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	id := cast.ToInt64(ctx.Param("id"))
	if id == 0 {
		httpResp.ErrorResponse(httpResp.ErrCodeInvalidParams, r)
		return
	}
	row, err := dao.SysDictDatum.Where(dao.SysDictDatum.DictCode.Eq(id)).Take()
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = row
}

func (c *dict) DataDelete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := servicesDict.DictData.Delete(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *dict) DataGet(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	id := ctx.Param("type")
	if len(id) == 0 {
		httpResp.ErrorResponse(httpResp.ErrCodeInvalidParams, r)
		return
	}
	data, err := servicesDict.Get(id)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *dict) TypeList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	var req entities.DictPageListReq
	if err := ctx.ShouldBind(&req); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	req.Page()

	data, err := servicesDict.DictType.List(req)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *dict) TypeOptionselect(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)

	req := entities.DictPageListReq{}
	req.PageNum = 1
	req.PageSize = 10000
	req.Page()
	data, err := servicesDict.DictType.List(req)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data.Rows
}

func (c *dict) TypeExport(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := servicesDict.DictType.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}
	httpResp.Export(ctx, b, "xlsx")
}

func (c *dict) RefreshCache(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := servicesDict.DictType.RefreshCache(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *dict) TypeDetail(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	id := cast.ToInt64(ctx.Param("id"))
	if id == 0 {
		httpResp.ErrorResponse(httpResp.ErrCodeInvalidParams, r)
		return
	}
	data, err := dao.SysDictType.Where(dao.SysDictType.DictID.Eq(id)).Take()
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *dict) TypeSave(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := servicesDict.DictType.Save(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *dict) TypeDelete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := servicesDict.DictType.Delete(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}
